home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / USDebtWatch / Source / Controller.m < prev    next >
Text File  |  1995-06-12  |  8KB  |  272 lines

  1. /* Generated by Interface Builder */
  2. /* NeXTSTEP interface by Erik Sowa (sowa@netcom.com) */
  3. /*  with help from Garance Alistair Drosehn (gad@eclipse.its.rpi.edu) */
  4.  
  5. #import <time.h>
  6. #import <defaults.h>
  7. #import <stdlib.h>
  8. #import <strings.h>
  9. #import <appkit/nextstd.h>
  10. #import <appkit/Application.h>        /* for loading nibs, etc. */
  11. #import <appkit/Panel.h>        /* for Info Panel stuff */
  12. #import <appkit/Form.h>         /* computeSCB89Debt writes to a
  13.                        FormCell */
  14. #import <appkit/TextFieldCell.h>    /* for Preference Panel */
  15. #import <dpsclient/dpsclient.h>        /* for timed entry stuff */
  16. #import "Controller.h"
  17.  
  18. /* declarations for xdebt-xcerpt */
  19. typedef double debt_t, pop_t;
  20. extern debt_t national_scb89debt_at();
  21. extern debt_t national_aj92debt_at();
  22. extern pop_t national_pop_at();
  23. extern void debt_to_string();
  24.  
  25. #define MINUPDATEINTERVAL 1.0     /* Do not update more than once a second. */
  26. #define MAXUPDATEINTERVAL 999.9    /* This limit is arbitrary. String sizes
  27.                    in setUpdateInterval depend on it. */
  28.  
  29. @implementation Controller
  30.  
  31. char buf[255];
  32. void strtoRect( char *, NXRect * );
  33.  
  34. - appDidInit:sender
  35. {
  36.  NXDefaultsVector defaults =
  37.     {
  38.     {"updateInterval","1.0"},        /* seconds between auto updates */
  39.     {"autoUpdate","1"},            /* 1=enabled, 0=disabled */
  40.     {"DebtWindowFrame",NULL},        /* location of Debt window */
  41.     {"PrefPanelFrame",NULL},        /* location of Preferences panel */
  42.     {NULL,NULL}
  43.     };
  44.     NXRect    tframe;
  45.  
  46.     /* Initialize DebtField at startup */
  47.     [self computeAJ92Debt:self];
  48.  
  49.     /* register and obtain valid app defaults */
  50.     NXRegisterDefaults([NXApp appName],defaults);
  51.     if ( NXGetDefaultValue([NXApp appName],"updateInterval") )
  52.     {
  53.     updateInterval =
  54.         atof(NXGetDefaultValue([NXApp appName],"updateInterval"));
  55.         updateInterval = MAX(updateInterval,MINUPDATEINTERVAL);
  56.         updateInterval = MIN(updateInterval,MAXUPDATEINTERVAL);
  57.     /* make sure pref panel reflects interval value.
  58.        there must be a better way than making two calls! */
  59.     [updateIntervalPrefField setDoubleValue:updateInterval];
  60.     [updateIntervalPrefSlider setDoubleValue:updateInterval];
  61.         }
  62.     if ( NXGetDefaultValue([NXApp appName],"autoUpdate") )
  63.         {
  64.         autoUpdate = atoi(NXGetDefaultValue([NXApp appName],"autoUpdate"));
  65.     if (autoUpdate != 0)
  66.         autoUpdate = 1;
  67.         /* make sure pref panel reflects auto update state */
  68.         [autoUpdatePref setState:autoUpdate];
  69.         }
  70.     if ( NXGetDefaultValue([NXApp appName],"DebtWindowFrame") )
  71.         {
  72.         strcpy(buf, NXGetDefaultValue([NXApp appName],"DebtWindowFrame"));
  73.     strtoRect(buf, &tframe);
  74.     [debtWindow moveTo:tframe.origin.x:tframe.origin.y];
  75.         }
  76.     if ( NXGetDefaultValue([NXApp appName],"PrefPanelFrame") )
  77.         {
  78.     strcpy(buf, NXGetDefaultValue([NXApp appName],"PrefPanelFrame"));
  79.     strtoRect(buf, &tframe);
  80.     [prefPanel moveTo:tframe.origin.x:tframe.origin.y];
  81.         }
  82.  
  83.    /* bring main window to front and make it key window.
  84.       among other things, this makes it visible (now that we've
  85.       processed the defaults setting for where it should appear) */
  86.     [debtWindow makeKeyAndOrderFront:self];
  87.     
  88.     /* register timed entry with window server */
  89.     updateEntry = DPSAddTimedEntry(updateInterval,(void *)_update,
  90.         self,NX_BASETHRESHOLD);
  91.  
  92.     return self;
  93. }
  94.  
  95. - appWillTerminate:sender
  96. {
  97.     if ( updateEntry )
  98.         {
  99.         DPSRemoveTimedEntry(updateEntry);
  100.         updateEntry = (DPSTimedEntry)0;
  101.         }
  102.     return self;
  103. }
  104.  
  105. - setUpdateInterval:sender
  106. {
  107.     /* get interval from pref panel field or slider and validate */
  108.     updateInterval = [sender doubleValue]; /* from Pref field or slider */
  109.     updateInterval = MAX(updateInterval,MINUPDATEINTERVAL);
  110.     updateInterval = MIN(updateInterval,MAXUPDATEINTERVAL);
  111.     sprintf(buf,"%.1f",updateInterval);
  112.     /* %.1f restricts precision, reread it for consistency */
  113.     updateInterval = atof(buf);
  114.     NXWriteDefault([NXApp appName],"updateInterval",buf);
  115.  
  116.     /* make sure pref panel reflects interval value.
  117.        needed because there are two ways to set it.
  118.        there must be a better way than making two calls! */
  119.     [updateIntervalPrefField setStringValue:buf];
  120.     [updateIntervalPrefSlider setStringValue:buf];
  121.     
  122.     /* set up the timed entry */
  123.     if (updateEntry)
  124.         {
  125.         DPSRemoveTimedEntry(updateEntry);
  126.         updateEntry = (DPSTimedEntry)0;
  127.         }
  128.     if (autoUpdate)
  129.         {
  130.         updateEntry = DPSAddTimedEntry(updateInterval,(void *)_update,
  131.         self,NX_BASETHRESHOLD);
  132.         }
  133.  
  134.     return self;
  135. }
  136.  
  137. - setAutoUpdate:sender
  138. {
  139.     /* get value from toggle switch button */
  140.     autoUpdate = [sender state]; /* check box state (0 or 1) */
  141.     sprintf(buf,"%d",autoUpdate);
  142.     NXWriteDefault([NXApp appName],"autoUpdate",buf);
  143.  
  144.     /* set up the timed entry */
  145.     if (updateEntry)
  146.         {
  147.         DPSRemoveTimedEntry(updateEntry);
  148.         updateEntry = (DPSTimedEntry)0;
  149.         }
  150.     if (autoUpdate)
  151.         {
  152.         updateEntry = DPSAddTimedEntry(updateInterval,(void *)_update,
  153.         self,NX_BASETHRESHOLD);
  154.         }
  155.     
  156.     return self;
  157. }
  158.  
  159. - showInfoPanel:sender
  160. {
  161.     if ( !infoPanel )
  162.         [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  163.     [infoPanel makeKeyAndOrderFront:self];
  164.     return self;
  165. }
  166.  
  167. - setWindowLocation:sender
  168. {
  169.     NXRect debtFrame, prefFrame;
  170.     
  171.     [debtWindow getFrame:&debtFrame];
  172.     sprintf(buf, "x%1.0f y%1.0f w%1.0f h%1.0f",
  173.         debtFrame.origin.x, debtFrame.origin.y,
  174.         debtFrame.size.width, debtFrame.size.height);
  175.     NXWriteDefault([NXApp appName], "DebtWindowFrame", buf);
  176.  
  177.     [prefPanel getFrame:&prefFrame];
  178.     sprintf(buf, "x%1.0f y%1.0f w%1.0f h%1.0f",
  179.         prefFrame.origin.x, prefFrame.origin.y,
  180.         prefFrame.size.width, prefFrame.size.height);
  181.     NXWriteDefault([NXApp appName], "PrefPanelFrame", buf);
  182.  
  183.     return self;
  184. }
  185.  
  186. - computeSCB89Debt:sender
  187. {
  188.     time_t now;
  189.     debt_t debt;
  190.     pop_t pop;
  191.     
  192.     now = time ((time_t*) 0);
  193.     debt = national_scb89debt_at(now);
  194.     pop = national_pop_at(now);
  195.  
  196.     debt_to_string (debt, buf);
  197.     [SCB89DebtString setStringValue:buf];
  198.     debt_to_string (debt/pop,buf);
  199.     [SCB89ShareString setStringValue:buf];
  200.     return self;
  201. }
  202.  
  203. - computeAJ92Debt:sender
  204. {
  205.     time_t now;
  206.     debt_t debt;
  207.     pop_t pop;
  208.     
  209.     now = time ((time_t*) 0);
  210.     debt = national_aj92debt_at(now);
  211.     pop = national_pop_at(now);
  212.  
  213.     debt_to_string (debt, buf);
  214.     [AJ92DebtString setStringValue:buf];
  215.     debt_to_string (debt/pop,buf);
  216.     [AJ92ShareString setStringValue:buf];
  217.     return self;
  218. }
  219.  
  220. @end
  221.  
  222. /* function to convert a character string (a defaults value) into
  223.    a NXRect frame.  The string is pretty much assumed to be valid,
  224.    though some minor amount of checking is done.  It accepts two
  225.    formats, the one used by NXFontPanelFrame (eg: "68 104 322 329")
  226.    and the one used by Cassandra (eg: "x68y104w322h329")
  227. */
  228. void strtoRect(char * inputString, NXRect * frame)
  229. {
  230.     char    *start, *next;
  231.     
  232.     frame->origin.y = frame->origin.y = 0.0;
  233.     frame->size.width = frame->size.height = 0.0;
  234.     
  235.     start = inputString;
  236.     if ( *start == ' ' ) start++;
  237.     if ( *start == 'x' ) start++;
  238.     if ( *start == '\0' ) return;
  239.     frame->origin.x = strtod(start, &next);
  240.     if ( start == next ) return;
  241.  
  242.     start = next;
  243.     if ( *start == ' ' ) start++;
  244.     if ( *start == 'y' ) start++;
  245.     if ( *start == '\0' ) return;
  246.     frame->origin.y = strtod(start, &next);
  247.     if ( start == next ) return;
  248.     
  249.     start = next;
  250.     if ( *start == ' ' ) start++;
  251.     if ( *start == 'w' ) start++;
  252.     if ( *start == '\0' ) return;
  253.     frame->size.width = strtod(start, &next);
  254.     if ( start == next ) return;
  255.     
  256.     start = next;
  257.     if ( *start == ' ' ) start++;
  258.     if ( *start == 'h' ) start++;
  259.     if ( *start == '\0' ) return;
  260.     frame->size.height = strtod(start, &next);
  261.     if ( start == next ) return;
  262.     
  263.     return;    
  264. }
  265.  
  266. /* function called by timed entry */
  267. void _update(DPSTimedEntry teNumber,double now,id self)
  268. {
  269.     [self computeAJ92Debt:self];
  270.     return;
  271. }
  272.